<?php

namespace app\modules\index\services;

use app\common\services\BaseService;
use app\models\MerchantCategory;

class MerchantCategoryService extends BaseService{

    //显示字段
    private const SELECT = ['cat_id','cat_name','thumbnail','parent_id'];

    /**
     * 获取推荐列表
     * @param $recommend
     * @param $pagesize
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getRecommendList($recommend,$pagesize){
        $query = MerchantCategory::find()
            ->select(self::SELECT)
            ->andWhere(['is_show'=>2,'parent_id'=>0])
            ->orderBy(['sort'=>SORT_ASC]);

        if(!empty($recommend)){
            $query->andWhere(['recommend'=>$recommend]);
        }
        if(!empty($pagesize)){
            $query->limit($pagesize);
        }

        return $query->all();
    }

    /**
     * 获取所有显示分类
     * @return array|\yii\db\ActiveRecord[]
     */
    protected static function getAll(){
        return MerchantCategory::find()->asArray(true)
            ->select(self::SELECT)
            ->andWhere(['is_show'=>2])
            ->orderBy(['sort'=>SORT_ASC])
            ->all();
    }

    /**
     * 获取下级列表(数组),不包含自身
     * @param $pid
     * @return array|[]
     */
    public static function getList($pid){
        $arr = self::getAll();
        return self::makeList($arr,$pid,0);
    }

    /**
     * 生成分类列表
     * @param $arr
     * @param int $pid
     * @param int $level
     * @return array|void
     */
    protected static function makeList($arr,$pid=0,$level=0)
    {
        static $child=[];

        foreach($arr as $k => $v)
        {
            if($v['parent_id']==$pid)
            {
                $v['level']=$level;
                $child[]=$v;
                unset($arr[$k]);
                self::makeList($arr,$v['cat_id'],$level+1);
            }
        }
        return $child;
    }

    /**
     * 获取下级和顶级树(树形),包含自身
     * @param $parent_id
     * @return array
     */
    public static function getTree($parent_id){
        $arr = self::getAll();
        $data = self::makeTree($arr,0,0);

        if(!empty($parent_id)){
            foreach ($data as $k=>$v){
                if($v['cat_id'] == $parent_id){
                    return $data[$k];
                }
            }
        }

        return $data;
    }

    /**
     * 生成树形分类树
     * @param $arr
     * @param int $pid
     * @param int $level
     * @param int $curlevel
     * @return array|void
     */
    protected static function makeTree($arr,$pid=0,$level=0)
    {
        $child =self::getListByPid($arr,$pid);

        if(empty($child))
        {
            return;
        }

        foreach($child as $k => $v)
        {
            if($current_child=self::makeTree($arr,$v['cat_id'],$level+1))
            {
                $child[$k]['child']=$current_child;
            }
        }

        return $child;
    }

    /**
     * 返回指定父分类的列表
     * @param $arr
     * @param $pid
     * @return array
     */
    protected static function getListByPid($arr,$pid)
    {
        $child=[];
        foreach($arr as $v)
        {
            if($v['parent_id']==$pid)
            {
                $child[]=$v;
            }
        }
        return $child;
    }
}